import provinces from './provinces.js';
import cities from './cities.js';
import areas from './areas.js';

const options = {

  getDic() {
    const list = provinces.map(province => ({
      label: province.name,
      value: province.code,
      children: this.getCity(province.code),
    }))

    // console.log(JSON.stringify(list))

    return list;
  },

  getCity(code) {
    return cities.filter(city => code === city.provinceCode)
      .map(city => ({
        label: city.name,
        value: city.code,
        children: this.getAreas(city.code),
      }))
  },

  getAreas(code) {
    return areas.filter(area => code === area.cityCode)
      .map(area => ({
        label: area.name,
        value: area.code,
      }))
  },


  //{"district":"东陵区",
  // "city":"沈阳市",
  // "address":"辽宁省沈阳市东陵区金卡路",
  // "longitude":123.465088,
  // "streetName":"金卡路",
  // "latitude":41.741531,
  // "streetNumber":"",
  // "province":"辽宁省"}

  getCityNoFromCityName(cityName) {
    const list = cities.filter(city => cityName == city.name);

    if (list.length === 1)
      return list[0].code;
    else
      return ''
  },

  getCityNameFromCityNo(cityName) {
    // areas.filter(areas => areaName === areas.name)

    const list = cities.filter(city => cityName == city.name);

    if (list.length === 1)
      return list[0].name;
    else
      return ''
  },

  getAreaNoFromAreaName(areaName) {
    // areas.filter(areas => areaName === areas.name)
    const provinceList = provinces.filter(province => areaName[0] == province.name);
    const provinceCode = provinceList[0].code;

    const cityList = cities.filter(city => provinceCode == city.provinceCode);
    const cityCode = cityList[0].code;

    const arealist = areas.filter(area => areaName[2] == area.name && area.cityCode == cityCode);

    if (arealist.length === 1)
      return list[0].code;
    else
      return ''
  },

  getListFromIndex(indexArray) {
    let multiArray;

    const provinceList = provinces
      .map((area, index) => (area.name))

    const provinceCode = provinces[indexArray[0]].code;

    const cityList = cities
      .filter(city => provinceCode == city.provinceCode)
      .map((city, index) => (city.name));
    const cityCode = cities.filter(city => provinceCode == city.provinceCode)[indexArray[1]].code;

    const arealist = areas
      .filter(area => area.cityCode == cityCode)
      .map((area, index) => (area.name));

    return [provinceList, cityList, arealist];
  },

  getCodeFromIndex(multiIndex) {
    const provinceCode = provinces[multiIndex[0]].code;

    const cityCode = cities.filter(city => provinceCode == city.provinceCode)[multiIndex[1]].code;

    const areaCode = areas.filter(area => area.cityCode == cityCode)[multiIndex[2]].code;

    return areaCode;
  },

  getIndexFromCode(code) {
    const provinceCode = code.substring(0, 2)
    const cityCode = code.substring(0, 4)
    const areaCode = code

    const provinceList = provinces
      .map((province, index) => {
        {
          console.log(JSON.stringify(province))
          return { index, ...province }
        }
      })
    const provinceFilterList = provinceList.filter(province => province.code == provinceCode);

    const provinceIndex = provinceFilterList[0].index;

    const cityIndex = cities
      .filter(city => city.provinceCode == provinceCode)
      .map((city, index) => ({ index, ...city }))
      .filter(city => city.code == cityCode)[0].index;

    const areaIndex = areas
      .filter(area => area.cityCode == cityCode)
      .map((area, index) => ({ index, ...area }))
      .filter(area => area.code == areaCode)[0].index;

    console.log([provinceIndex, cityIndex, areaIndex])

    return [provinceIndex, cityIndex, areaIndex];
  },

  getListFromIndexWithData(data, indexArray) {
    if (data == null) return;
    let multiArray;
    const provinceList = data.map(item => item.name)
    let cityList;
    let arealist;
    if (data[indexArray[0]].children == null){
      cityList = [];
      arealist = [];
    } else {
      cityList = data[indexArray[0]].children.map(item => item.name);
      if (data[indexArray[0]].children[indexArray[1]].children == null) {
        arealist = [];
      } else {
        arealist = data[indexArray[0]].children[indexArray[1]].children.map(item => item.name);
      }
    }
   
    return [provinceList, cityList, arealist];
  },

  getCodeFromIndexWithData(data, multiIndex) {
    console.log(multiIndex);
    if (data == null) return;
    if (data[multiIndex[0]].children === undefined){
      return data[multiIndex[0]].code;
    } else if (data[multiIndex[0]].children[multiIndex[1]].children === undefined){
      return data[multiIndex[0]].children[multiIndex[1]].code;
    } else {
      return data[multiIndex[0]].children[multiIndex[1]].children[multiIndex[2]].code;
    }
  },

  getIndexFromCodeWithData(data, code) {
    if (data == null) return;
    var multiIndex = [0, 0, 0];
    data.map((provinceItem, provinceIndex) => {
      if (provinceItem.children)
        provinceItem.children.map((cityItem, cityIndex) => {
          if (cityItem.children)
            cityItem.children.map((areaItem, areaIndex) => {
              if (areaItem.code == code) {
                multiIndex = [provinceIndex, cityIndex, areaIndex]
              }
            })
        })
    })

   return multiIndex;
  }
}


// export const getObj = (code) => {
//     if (code == null) {
//         return {};
//     }
//     let array;
//     if (code.length === 2) {
//         array = provinces;
//     } else if (code.length === 4) {
//         array = cities;
//     } else if (code.length === 6) {
//         array = areas;
//     } else {
//         array = [];
//     }
//     for (const value of array) {
//         if (value.code === code) {
//             return {label: value.name, value: value.code, children: value.children};
//         }
//     }
//     return {};
// };

export default options;